//
//  YFNavigationBar.m
//  CustomNavigationBarDemo
//
//  Created by Winny Zhang on 2019/3/24.
//  Copyright © 2019 Winny Zhang. All rights reserved.
//

#import "YFNavigationBar.h"

#define YF_STATUS_BAR_HEIGHT ([UIApplication sharedApplication].statusBarFrame.size.height)
#define YF_SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define YF_SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define YF_BACK_BUTTON_TAG (9999)
#define YF_LEFT_CUSTOM_VIEW_TAG (10000)
#define YF_TITLE_BUTTON_TAG (15001)
#define YF_TITLE_VIEW_TAG (15000)
#define YF_RIGHT_CUSTOM_VIEW_TAG (30000)

@interface YFNavigationBar ()
/**
 背景 ImageView
 */
@property (nonatomic, strong, nullable) UIImageView *imageView;

/**
 Content View
 */
@property (nonatomic, strong, nonnull) UIView *contentView;

/**
 显示 Title
 */
@property (nonatomic, strong, nullable) UIButton *titleButton;

@end

@implementation YFNavigationBar

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setupSubViews];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubViews];
    }
    return self;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupSubViews];
    }
    return self;
}


- (void)setupSubViews {
    self.userInteractionEnabled = YES;
    [self setupBackgroundImageView];
    [self setupContentView];
    [self setupTitleButton];
    [self setupBackButton];
}

/**
 设置背景图片
 */
- (void)setupBackgroundImageView {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.frame];
    imageView.userInteractionEnabled = YES;
    [self addSubview:imageView];
    self.imageView = imageView;
}

/**
 设置 Content View
 */
- (void)setupContentView {
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, YF_STATUS_BAR_HEIGHT, YF_SCREEN_WIDTH, 44)];
    contentView.backgroundColor = [UIColor clearColor];
    contentView.userInteractionEnabled = YES;
    [self addSubview:contentView];
    self.contentView = contentView;
}

/**
 设置标题
 */
- (void)setupTitleButton {
    UIButton *titleButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    titleButton.tag = YF_TITLE_BUTTON_TAG;
    titleButton.frame = CGRectZero;
    
    [titleButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [titleButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateHighlighted)];
    [titleButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
    [titleButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateDisabled)];
    titleButton.titleLabel.font = [UIFont fontWithName:PingFangSC_Medium size:19];
    self.titleButton = titleButton;
}

/**
 设置返回按钮
 */
- (void)setupBackButton {
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.tag = YF_BACK_BUTTON_TAG;
    backButton.frame = CGRectZero;
    [self.contentView addSubview:backButton];
    self.backButton = backButton;
}

- (CGSize)intrinsicContentSize {
    return UILayoutFittingExpandedSize;
}

- (CGRect)frame {
    return CGRectMake(0, 0, YF_SCREEN_WIDTH, YF_STATUS_BAR_HEIGHT + 44 + 10);
}

#pragma mark - Setter
/**
 设置背景图片
 */
- (void)setBackgroundImage:(UIImage *)backgroundImage {
    _backgroundImage = backgroundImage;
    self.imageView.image = backgroundImage;
}

/**
 设置标题
 */
- (void)setTitle:(NSString *)title {
    [self.titleButton removeFromSuperview];
    _title = title;
    [self.titleButton setTitle:title forState:(UIControlStateNormal)];
    [self.titleButton sizeToFit];
    self.titleButton.center = CGPointMake(self.contentView.frame.size.width * 0.5,  self.contentView.frame.size.height * 0.5);
    [self.contentView addSubview:self.titleButton];
}

/**
 设置标题字体
 */
- (void)setTitleFont:(UIFont *)titleFont {
    _titleFont = titleFont;
    self.titleButton.titleLabel.font = titleFont;
}

/**
 设置标题颜色
 */
- (void)setTitleColor:(UIColor *)titleColor {
    _titleColor = titleColor;
    [self.titleButton setTitleColor:titleColor forState:(UIControlStateNormal)];
    [self.titleButton setTitleColor:titleColor forState:(UIControlStateHighlighted)];
    [self.titleButton setTitleColor:titleColor forState:(UIControlStateSelected)];
    [self.titleButton setTitleColor:titleColor forState:(UIControlStateDisabled)];
}

/**
 设置返回按钮 Normal Image
 */
- (void)setBackImage:(UIImage *)backImage {
    _backImage = backImage;
    [self.backButton setImage:backImage forState:(UIControlStateNormal)];
    self.backButton.frame = CGRectMake(0, 0, 30, 44);
}

/**
 设置返回按钮 高亮 Image
 */
- (void)setBackHighlightImage:(UIImage *)backHighlightImage {
    _backHighlightImage = backHighlightImage;
    [self.backButton setImage:backHighlightImage forState:(UIControlStateHighlighted)];
    self.backButton.frame = CGRectMake(0, 0, 30, 44);
}

/**
 设置左边的 Custom View
 */
- (void)setLeftCustomView:(UIView *)leftCustomView {
    [_leftCustomView removeFromSuperview];
    leftCustomView.tag = YF_LEFT_CUSTOM_VIEW_TAG;
    _leftCustomView = leftCustomView;
    CGSize customSize = leftCustomView.frame.size;
    // 计算 leftCustomeView frame
    CGRect customFrame = CGRectZero;
    customFrame.size.height = 44;
    for (UIView *subView in self.contentView.subviews) {
        // 有 title 或者 titleView
        if (subView.tag == YF_TITLE_BUTTON_TAG || subView.tag == YF_TITLE_VIEW_TAG) {
            for (UIView *subView1 in self.contentView.subviews) {
                // 有返回按钮，则起点为返回按钮的最右侧，宽度为返回按钮最右侧到 title（titleView）之间的距离
                if (subView1.tag == YF_BACK_BUTTON_TAG) {
                    customFrame.origin.x = CGRectGetMaxX(subView1.frame);
                    customFrame.size.width = CGRectGetMinX(subView.frame) - CGRectGetMaxX(subView1.frame);
                } else {
                    // 没有返回按钮，则起点为0，宽度 0 到 title（titleView）之间的距离
                    customFrame.origin.x = 0;
                    customFrame.size.width = CGRectGetMinX(subView.frame);
                }
            }
            break;
        } else {
            // 没有 title 和 titleView，起点为0，宽度为 contentView 宽度的一半
            customFrame.origin.x = 0;
            customFrame.size.width = self.contentView.frame.size.width * 0.5;
        }
    }
    // 如果 leftCustomView 的宽度 小于 计算出的最大宽度，使用 leftCustomView 本身宽度
    if (customFrame.size.width > customSize.width) {
        customFrame.size.width = customSize.width;
    }
    leftCustomView.frame = customFrame;
    [self.contentView addSubview:leftCustomView];
    // 居中
    CGPoint customCenter = leftCustomView.center;
    customCenter.y = self.contentView.frame.size.height * 0.5;
    leftCustomView.center = customCenter;
}

/**
 设置右边的 Custome View
 */
- (void)setRightCustomView:(UIView *)rightCustomView {
    [_rightCustomView removeFromSuperview];
    rightCustomView.tag = YF_RIGHT_CUSTOM_VIEW_TAG;
    _rightCustomView = rightCustomView;
    CGSize customSize = rightCustomView.frame.size;
    // 计算 rightCustomeView frame
    CGRect customFrame = CGRectZero;
    customFrame.size.height = 44;
    for (UIView *subView in self.contentView.subviews) {
        // 有 title 或者 titleView
        if (subView.tag == YF_TITLE_BUTTON_TAG || subView.tag == YF_TITLE_VIEW_TAG ) {
            customFrame.origin.x = CGRectGetMaxX(subView.frame);
            customFrame.size.width = YF_SCREEN_WIDTH - CGRectGetMaxX(subView.frame);
        } else {
            // 没有 title 和 titleView，起点为 contentView 宽度的一半，宽度为 contentView 宽度的一半
            customFrame.origin.x = self.contentView.frame.size.width * 0.5;
            customFrame.size.width = self.contentView.frame.size.width * 0.5;
        }
    }
    // 如果 leftCustomView 的宽度 小于 计算出的最大宽度，使用 leftCustomView 本身宽度
    if (customFrame.size.width > customSize.width) {
        customFrame.size.width = customSize.width;
        customFrame.origin.x = self.contentView.frame.size.width - customSize.width;
    }
    rightCustomView.frame = customFrame;
    [self.contentView addSubview:rightCustomView];
    // 居中
    CGPoint customCenter = rightCustomView.center;
    customCenter.y = self.contentView.frame.size.height * 0.5;
    rightCustomView.center = customCenter;
}


/**
 设置中间 Title View
 */
- (void)setTitleView:(UIView *)titleView {
    [_titleButton removeFromSuperview];
    [_titleView removeFromSuperview];
    titleView.tag = YF_TITLE_VIEW_TAG;
    _titleView = titleView;
    titleView.userInteractionEnabled = YES;
    CGSize customSize = titleView.frame.size;
    CGRect customFrame = CGRectZero;
    customFrame.size.height = 44;
    for (UIView *subView in self.contentView.subviews) {
        // 如果有左视图，只需要判断是否有右视图
        if (subView.tag == YF_LEFT_CUSTOM_VIEW_TAG) {
            for (UIView *subView1 in self.contentView.subviews) {
                if (subView1.tag == YF_RIGHT_CUSTOM_VIEW_TAG) {
                    if (CGRectGetMaxX(subView.frame) >= YF_SCREEN_WIDTH - CGRectGetMinX(subView1.frame)) {
                        customFrame.origin.x = (YF_SCREEN_WIDTH - CGRectGetMaxX(subView.frame) * 2) * 0.5;
                        customFrame.size.width = YF_SCREEN_WIDTH - CGRectGetMaxX(subView.frame) * 2;
                    } else {
                        customFrame.origin.x = (YF_SCREEN_WIDTH - (YF_SCREEN_WIDTH - CGRectGetMinX(subView1.frame)) * 2) * 0.5;
                        customFrame.size.width = YF_SCREEN_WIDTH - (YF_SCREEN_WIDTH - CGRectGetMinX(subView1.frame)) * 2;
                    }
                } else {
                    customFrame.origin.x = (YF_SCREEN_WIDTH - CGRectGetMaxX(subView.frame) * 2) * 0.5;
                    customFrame.size.width = YF_SCREEN_WIDTH - CGRectGetMaxX(subView.frame) * 2;
                }
            }
            // 如果有右视图，需要判断是否有返回按钮、是否有左视图
        } else if (subView.tag == YF_RIGHT_CUSTOM_VIEW_TAG) {
            
            for (UIView *subView1 in self.contentView.subviews) {
                // 如果有左视图或者有返回按钮
                if (subView1.tag == YF_LEFT_CUSTOM_VIEW_TAG || subView1.tag == YF_BACK_BUTTON_TAG) {
                    if (CGRectGetMaxX(subView1.frame) >= YF_SCREEN_WIDTH - CGRectGetMinX(subView.frame)) {
                        customFrame.origin.x = (YF_SCREEN_WIDTH - CGRectGetMaxX(subView1.frame) * 2) * 0.5;
                        customFrame.size.width = YF_SCREEN_WIDTH - CGRectGetMaxX(subView1.frame) * 2;
                    } else {
                        customFrame.origin.x = (YF_SCREEN_WIDTH - (YF_SCREEN_WIDTH - CGRectGetMinX(subView.frame)) * 2) * 0.5;
                        customFrame.size.width = YF_SCREEN_WIDTH - (YF_SCREEN_WIDTH - CGRectGetMinX(subView.frame)) * 2;
                    }
                } else {
                    /// 没有左视图且没有返回按钮
                    customFrame.origin.x = (YF_SCREEN_WIDTH - (YF_SCREEN_WIDTH - CGRectGetMinX(subView.frame)) * 2) * 0.5;
                    customFrame.size.width = YF_SCREEN_WIDTH - (YF_SCREEN_WIDTH - CGRectGetMinX(subView.frame)) * 2;
                }
            }
            // 如果有返回按钮，需要判断是否有左视图、是否有右视图
        } else if (subView.tag == YF_BACK_BUTTON_TAG) {
            for (UIView *subView1 in self.contentView.subviews) {
                // 如果有左视图
                if (subView1.tag == YF_LEFT_CUSTOM_VIEW_TAG ) {
                    for (UIView *subView2 in self.contentView.subviews) {
                        // 如果有右视图
                        if (subView2.tag == YF_RIGHT_CUSTOM_VIEW_TAG) {
                            if (CGRectGetMaxX(subView1.frame) >= YF_SCREEN_WIDTH - CGRectGetMinX(subView2.frame)) {
                                customFrame.origin.x = (YF_SCREEN_WIDTH - CGRectGetMaxX(subView1.frame) * 2) * 0.5;
                                customFrame.size.width = YF_SCREEN_WIDTH - CGRectGetMaxX(subView1.frame) * 2;
                            } else {
                                customFrame.origin.x = (YF_SCREEN_WIDTH - (YF_SCREEN_WIDTH - CGRectGetMinX(subView2.frame)) * 2) * 0.5;
                                customFrame.size.width = YF_SCREEN_WIDTH - (YF_SCREEN_WIDTH - CGRectGetMinX(subView2.frame)) * 2;
                            }
                            // 没有右视图
                        } else {
                            customFrame.origin.x = (YF_SCREEN_WIDTH - (YF_SCREEN_WIDTH - CGRectGetMaxX(subView.frame)) * 2) * 0.5;
                            customFrame.size.width = YF_SCREEN_WIDTH - (YF_SCREEN_WIDTH - CGRectGetMaxX(subView.frame)) * 2;
                        }
                    }
                    // 如果有右视图
                } else if (subView1.tag == YF_RIGHT_CUSTOM_VIEW_TAG) {
                    for (UIView *subView2 in self.contentView.subviews) {
                        // 如果有左视图
                        if (subView2.tag == YF_LEFT_CUSTOM_VIEW_TAG) {
                            if (CGRectGetMaxX(subView2.frame) >= YF_SCREEN_WIDTH - CGRectGetMinX(subView1.frame)) {
                                customFrame.origin.x = (YF_SCREEN_WIDTH - CGRectGetMaxX(subView2.frame) * 2) * 0.5;
                                customFrame.size.width = YF_SCREEN_WIDTH - CGRectGetMaxX(subView2.frame) * 2;
                            } else {
                                customFrame.origin.x = (YF_SCREEN_WIDTH - (YF_SCREEN_WIDTH - CGRectGetMinX(subView1.frame)) * 2) * 0.5;
                                customFrame.size.width = YF_SCREEN_WIDTH - (YF_SCREEN_WIDTH - CGRectGetMinX(subView1.frame)) * 2;
                            }
                            // 没有左视图
                        } else {
                            customFrame.origin.x = (YF_SCREEN_WIDTH - (YF_SCREEN_WIDTH - CGRectGetMaxX(subView.frame)) * 2) * 0.5;
                            customFrame.size.width = YF_SCREEN_WIDTH - (YF_SCREEN_WIDTH - CGRectGetMaxX(subView.frame)) * 2;
                        }
                    }
                }
            }
        } else {
            
        }
    }
    
    /// 如果什么都没有
    if (self.contentView.subviews.count == 0) {
        customFrame.origin.x = 0;
        customFrame.size.width = YF_SCREEN_WIDTH;
    }
    
    if(customFrame.size.width > customSize.width) {
        customFrame.size.width = customSize.width;
        customFrame.origin.x = (YF_SCREEN_WIDTH - customSize.width) * 0.5;
    }
    titleView.frame = customFrame;
    [self.contentView addSubview:titleView];
    // 设置居中
    CGPoint customCenter = titleView.center;
    customCenter.y = self.contentView.frame.size.height * 0.5;
    titleView.center = customCenter;
}


@end
